<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output
        encoding="utf-8"
        indent="yes"
        media-type="text/xml"
        omit-xml-declaration="yes"
        standalone="no" />

    <xsl:param name="cmd" />

    <xsl:include href="./inc/App.xslt"/>
    <xsl:include href="./inc/Form.xslt"/>

    <xsl:template match="/">
        <div>
        <xsl:choose>
            <xsl:when test="/InternalMessage"><xsl:call-template name="app_message" /></xsl:when>

            <xsl:when test="($cmd='GetDocumentForEdit') or ($cmd='InsertDocument') or ($cmd='NewDocument') or ($cmd='SetDocument')"><xsl:call-template name="form" /></xsl:when>

            <xsl:otherwise>
                <h1>Unknown Input</h1>
                <p>Command: <xsl:value-of select="$cmd"/></p>
                <p>Server Response:</p>
                <p><textarea id="RawServerResponse"><xsl:copy-of select="/" /></textarea></p>
            </xsl:otherwise>
        </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template name="form">

        <xsl:for-each select="//Branches/Branch">
        <div class="Branch">
            <xsl:for-each select="./SegmentBranch/Segment">
            <xsl:if test="position() = 1"><span class="BranchDelimiter">//</span></xsl:if><a class="SegmentLink" href="#" id="{concat('item_',position(),'a_',./SegmentId)}" title="{./SegmentName}"><xsl:value-of select="./SegmentName"/></a><span class="BranchDelimiter">/</span>
            </xsl:for-each>
        </div>
        </xsl:for-each>
        <xsl:if test="string-length(/ServerData/Document/DocumentId) &gt; 0">
        <div class="Branch">
            <a class="DocumentLink" href="#" id="{concat('item_',position(),'a_',/ServerData/Document/DocumentId)}" title="{/ServerData/Document/Title}">Document <xsl:value-of select="/ServerData/Document/DocumentShortName"/></a><span class="BranchDelimiter">/</span>
        </div>
        </xsl:if>

        <xsl:choose>
            <xsl:when test="string-length(/ServerData/Document/DocumentId) &gt; 0">
        <h1 class="DataEntryForm"><xsl:copy-of select="/ServerData/Document/Title/node()"/></h1>
            </xsl:when>
            <xsl:otherwise>
        <h1 class="DataEntryForm">New Document</h1>
            </xsl:otherwise>
        </xsl:choose>
        <form action="#" class="DataEntryForm" id="DocumentForm">
            <xsl:variable name="related_items">
                <xsl:choose>
                    <xsl:when test="count(//ChildFragments/Fragment) &gt; 1">
                        <xsl:value-of select="count(//ChildFragments/Fragment)"/> Related Items
                    </xsl:when>
                    <xsl:when test="count(//ChildFragments/Fragment) = 1">One Related Item</xsl:when>
                    <xsl:otherwise>No Related Items</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <div class="CalendarBlock" id="CalendarBlock">&#160;</div>
            <div id="DataFormTabView" class="yui-navset">
                <ul class="yui-nav">
                    <li class="selected"><a class="TabAnchor" href="#ItemOne"><em>General</em></a></li>
                    <li><a class="TabAnchor" href="#ItemTwo"><em>Segments</em></a></li>
                    <li><a class="TabAnchor" href="#ItemThree"><em>Advanced</em></a></li>
                    <li><a class="TabAnchor" href="#ItemFour"><em>Keywords</em></a></li>
                    <li><a class="TabAnchor" href="#ItemFive"><em><xsl:value-of select="$related_items"/></em></a></li>
                </ul>
                <div class="yui-content">
                    <div id="ItemOne">
                        <xsl:if test="string-length(/ServerData/Document/DocumentId) &gt; 0">
                            <div class="IdentifierBlock" title="{concat('Database Identifier: ',/ServerData/Document/DocumentId)}"><xsl:value-of select="/ServerData/Document/DocumentId" /></div>
                            <input id="DocumentId" type="hidden" value="{/ServerData/Document/DocumentId}" />
                        </xsl:if>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/Title" /><xsl:with-param name="type" select="'textarea_node'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/DocumentShortName"/><xsl:with-param name="caption" select="'Short Name'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/FileName"/><xsl:with-param name="caption" select="'File Name'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/Path"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/CreateDate"/><xsl:with-param name="caption" select="'Create Date'"/><xsl:with-param name="type" select="'datepicker'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/ModificationDate"/><xsl:with-param name="caption" select="'Modification Date'"/><xsl:with-param name="type" select="'datepicker'" /></xsl:call-template>
                        <xsl:if test="count(/ServerData/Document/Template) &gt; 0">
                        <xsl:call-template name="form_row">
                            <xsl:with-param name="n" select="//Template"/>
                            <xsl:with-param name="type" select="'option'" />
                            <xsl:with-param name="option_display_name" select="'Title'" />
                            <xsl:with-param name="option_selected_value" select="'TemplateId'" />
                            <xsl:with-param name="option_value"  select="'DocumentId'"/>
                        </xsl:call-template>
                        </xsl:if>
                    </div>
                    <div id="ItemTwo">
                        <div id="DocumentSegments">
                            <ul>
                                <xsl:for-each select="//Segments/Segment[not(ParentSegmentId)]">
                                    <xsl:sort select="SegmentName" />
                                    <xsl:variable name="id" select="./SegmentId" />
                                    <li>
                                        <input class="RadioButton" id="{concat('Segment',$id)}" name="SegmentId" type="radio" value="{$id}">
                                            <xsl:if test="/ServerData/SubSets/DocumentSegments/SegmentDocument[SegmentId=$id]"><xsl:attribute name="checked"><xsl:text>checked</xsl:text></xsl:attribute></xsl:if>
                                            <xsl:if test="/ServerData/Document[SegmentId=$id]"><xsl:attribute name="checked"><xsl:text>checked</xsl:text></xsl:attribute></xsl:if>
                                        </input>
                                        <xsl:value-of select="./SegmentName" />
                                        <xsl:if test="//Segment[ParentSegmentId=$id]">
                                        <ul>
                                            <xsl:apply-templates select="//Segment[ParentSegmentId=$id]"/>
                                        </ul>
                                        </xsl:if>
                                    </li>
                                </xsl:for-each>
                            </ul>
                        </div>
                    </div>
                    <div id="ItemThree">
                        <xsl:choose>
                        <xsl:when test="count(//Templates/Template) &gt; 0">
                        <xsl:call-template name="form_row">
                            <xsl:with-param name="n" select="//Templates/Template"/>
                            <xsl:with-param name="type" select="'option'" />
                            <xsl:with-param name="maxlength" select="32" />
                            <xsl:with-param name="option_display_name" select="'Title'" />
                            <xsl:with-param name="option_select_id" select="'TemplateId'" />
                            <xsl:with-param name="option_selected_value" select="ServerData/Document/TemplateId" />
                            <xsl:with-param name="option_value"  select="'DocumentId'"/>
                            <xsl:with-param name="caption" select="'Template'"/>
                        </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                        <input id="TemplateId" type="hidden" value="" />
                        </xsl:otherwise>
                        </xsl:choose>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/IsActive" /><xsl:with-param name="caption" select="'Active?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/IsRoot" /><xsl:with-param name="caption" select="'Root?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/SortOrdinal" /><xsl:with-param name="caption" select="'Sort Ordinal'"/><xsl:with-param name="maxlength" select="'4'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/Tag" /><xsl:with-param name="type" select="'textarea'" /><xsl:with-param name="caption" select="'Tag'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Document/ClientId" /><xsl:with-param name="caption" select="'Client ID'"/></xsl:call-template>
                    </div>
                    <div id="ItemFour">
                        <div class="FormRow">
                            <label for="DocumentKeywords">Document Keywords:</label>
                            <textarea class="TextArea" id="DocumentKeywords"><xsl:if test="count(//Webkeyword) = 0">&#160;</xsl:if>
                                <xsl:for-each select="//Webkeyword">
                                    <xsl:choose>
                                        <xsl:when test="position() = 1"><xsl:value-of select="./KeywordValue"/></xsl:when>
                                        <xsl:otherwise>, <xsl:value-of select="./KeywordValue"/></xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </textarea>
                        </div>
                    </div>
                    <div id="ItemFive">
                        <xsl:choose>
                        <xsl:when test="(count(//ChildFragments/Fragment) &gt; 0)">
                            <xsl:apply-templates select="//ChildFragments"><xsl:with-param name="id" select="/ServerData/Document/DocumentId" /></xsl:apply-templates>
                        </xsl:when>
                        <xsl:otherwise>[No child nodes]</xsl:otherwise>
                        </xsl:choose>
                    </div>
                </div>
            </div>
            <div class="EditCommands">
                <button type="reset">Cancel</button>
                <xsl:if test="string-length(//DocumentId) &gt; 0"><button id="DeleteDocument">Delete</button></xsl:if>
                <xsl:variable name="save_id">
                <xsl:choose>
                    <xsl:when test="string-length(//DocumentId) &gt; 0">SaveDocument</xsl:when>
                    <xsl:otherwise>SaveNewDocument</xsl:otherwise>
                </xsl:choose>
                </xsl:variable>
                <button id="{$save_id}">Save</button>
            </div>
        </form>

    </xsl:template>

    <xsl:template match="ChildFragments">
        <xsl:param name="id" />

        <xsl:if test="count(Fragment[DocumentId=$id]) &gt; 0">
        <h3>Child Fragments</h3>
        <ul id="ChildFragments">
            <xsl:for-each select="Fragment[DocumentId=$id]">
            <li>
                <xsl:variable name="fragment_name_prefix">
                    <xsl:choose>
                        <xsl:when test="(string-length(./FragmentWrapper/PrevFragment) &gt; 0)"><xsl:value-of select="concat(./FragmentWrapper/PrevFragment,', ')"/></xsl:when>
                        <xsl:otherwise></xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="fragment_name_suffix">
                    <xsl:choose>
                        <xsl:when test="(string-length(./FragmentWrapper/NextFragment) &gt; 0)"><xsl:value-of select="concat(', ',./FragmentWrapper/NextFragment)"/></xsl:when>
                        <xsl:otherwise></xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="fragment_name">
                    <xsl:choose>
                        <xsl:when test="string-length(./FragmentDisplayName) &gt; 0"><xsl:value-of select="./FragmentDisplayName"/></xsl:when>
                        <xsl:when test="string-length(./FragmentName) &gt; 0"><xsl:value-of select="./FragmentName"/></xsl:when>
                        <xsl:when test="string-length(./ClientId) &gt; 0"><xsl:value-of select="./ClientId"/></xsl:when>
                    </xsl:choose>
                </xsl:variable>
                <a class="FragmentLink" href="#" id="{concat('item_',position(),'_',./FragmentId)}">[<xsl:value-of select="./FragmentId"/>] <xsl:value-of select="concat($fragment_name_prefix,$fragment_name,$fragment_name_suffix)"/></a>
            </li>
            </xsl:for-each>
        </ul>
        </xsl:if>
    </xsl:template>

    <xsl:template match="Segment">
        <xsl:for-each select=".">
            <xsl:sort select="./SegmentName" />
            <xsl:variable name="id" select="./SegmentId" />
        <li>
            <input class="RadioButton" id="{concat('Segment',$id)}" name="SegmentId" type="radio" value="{$id}">
                <xsl:if test="/ServerData/SubSets/DocumentSegments/SegmentDocument[SegmentId=$id]"><xsl:attribute name="checked"><xsl:text>checked</xsl:text></xsl:attribute></xsl:if>
                <xsl:if test="/ServerData/Document[SegmentId=$id]"><xsl:attribute name="checked"><xsl:text>checked</xsl:text></xsl:attribute></xsl:if>
            </input>
            <xsl:value-of select="./SegmentName" /><xsl:apply-templates select="./Segment"/>
            <xsl:if test="//Segment[ParentSegmentId=$id]">
            <ul>
                <xsl:apply-templates select="//Segment[ParentSegmentId=$id]"/>
            </ul>
            </xsl:if>
        </li>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet> 
